package com.zzk.www.nettyServer.tool;

import com.zzk.www.domain.User;
import io.netty.channel.Channel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by zzk on 2017/12/22.
 */
public class ChannelUserHolder {

    public static Map<Channel, User> userMap = new HashMap<Channel, User>();


    public static void put(Channel channel, User user) {
        userMap.put(channel, user);
    }

    public static User get(Channel channel) {
        return userMap.get(channel);
    }

    public static void remove(Channel channel) {
        userMap.remove(channel);
    }

    public static Set<Channel> keys() {
        return userMap.keySet();
    }

    //根据用户id查找channel
    public static Channel getChannelByUserId(String userId) {
        for (Map.Entry<Channel, User> channelUserEntry : userMap.entrySet()) {
            if (channelUserEntry.getValue().getUserId().equals(userId)) {
                return channelUserEntry.getKey();
            }
        }
        return null;
    }

    //根据socket地址查找channel
    public static Channel getChannelByAddress(String address) {
        for (Map.Entry<Channel, User> channelUserEntry : userMap.entrySet()) {
            if (channelUserEntry.getValue().getAddress().equals(address)) {
                return channelUserEntry.getKey();
            }
        }
        return null;
    }



}
